package cn.edu.scau.cmi.houzhicong.factoryPattern.gui;

import cn.edu.scau.cmi.houzhicong.factoryPattern.domain.Insurance;
import cn.edu.scau.cmi.houzhicong.factoryPattern.factory.*;
import javafx.collections.FXCollections;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.stage.FileChooser;
import javafx.stage.Stage;

import java.io.File;
import java.net.URL;
import java.util.ResourceBundle;

public class FactoryController implements Initializable {
    private final String[] insurancesKind = new String[]{"无", "身体受伤", "汽车损坏", "人员伤亡", "多种事故"};
    @FXML
    private ChoiceBox choice;
    @FXML
    private Button addPicButton;
    @FXML
    private Button showButton;
    @FXML
    private TextField nameText;
    @FXML
    private TextField stuNumText;
    @FXML
    private ImageView imageView;
    @FXML
    private TextArea infoArea;
    private String ins = null;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        choice.setItems(FXCollections.observableArrayList("无", "身体受伤", "汽车损坏", "人员伤亡", "多种事故"));
        choice.getSelectionModel().selectedIndexProperty().addListener((observable, oldValue, newValue) -> {
            ins = insurancesKind[newValue.intValue()];
        });
    }

    public void addPic() {
        FileChooser fileChooser = new FileChooser();
        fileChooser.setTitle("选择一个图片文件");
        fileChooser.setSelectedExtensionFilter(new FileChooser.ExtensionFilter("Image", "*.jpg", "*.jpeg", "*.png"));
        Stage exStage = null;
        File file = fileChooser.showOpenDialog(exStage);
//        System.out.println(file.getAbsolutePath());
        if (file != null) {
            imageView.setImage(new Image("file:" + file.getAbsolutePath(), true));
        }
    }

    public void createInsurance() {
        String name = nameText.getText();
        String stuNum = stuNumText.getText();
        InsuranceFactory factory = null;
        if (ins != null) {
            switch (ins) {
                case "身体受伤":
                    factory = new BodilyInjuryFactory();
                    break;
                case "汽车损坏":
                    factory = new CarDamageFactory();
                    break;
                case "人员伤亡":
                    factory = new HumanCasualtyFactory();
                    break;
                case "多种事故":
                    factory = new MultipleAccidentsFactory();
                    break;
            }
        }


        if (factory == null) {
            infoArea.setText("请选择保险类型！");
        } else {
            Insurance insurance = factory.createInsurance(name, stuNum, imageView.getImage());
            infoArea.setText(insurance.toString());
        }
    }

}
